package source.Control 
{
    import flash.display.BitmapData;
    import flash.display.BlendMode;
    import flash.display.GradientType;
    import flash.display.Graphics;
    import net.flashpunk.Entity;
    import net.flashpunk.FP;
    import net.flashpunk.graphics.Graphiclist;
    import net.flashpunk.graphics.Image;
    import net.flashpunk.tweens.misc.NumTween;
    import net.flashpunk.utils.Ease;
    import net.flashpunk.World;
    import source.Assets;
    import source.Global;
	
	/**
	 * This object displays the FlashPunk splash screen.
	 */
	public class Presentacion extends Entity
	{
		/**
		 * Image objects.
		 */
		public var list:Graphiclist;
        public var explosiveBrains:Image = new Image(Assets.PRES_EXPLOSIVEBRAINS);
        public var explosiveGame:Image = new Image(Assets.PRES_EXPLOSIVEGAME);
		public var lines:Image;
		public var fade:Image = Image.createRect(FP.width, FP.height, 0);
		
		/**
		 * Tween information.
		 */
		public var fader:NumTween = new NumTween(faderEnd);
        public var times:uint = 1; //numero de imágenes que quedan por presentar
		public var leftX:int;
		public var rightX:int;
        
        /**
		 * Fade in/out time and logo spinning time.
		 */
		private var _fadeTime:Number;
        private var _pauseTimer:uint;
		private var _onComplete:*;
		
		/**
		 * Constructor, start the splash animation.
		 */
		public function Presentacion(color:uint = 0xFF3366, bgColor:uint = 0x202020) {            
			// Create the lines image.
			var data:BitmapData = new BitmapData(FP.width, FP.height, false, 0x353535);
			var	g:Graphics = FP.sprite.graphics;
			g.clear();
			g.beginGradientFill(GradientType.RADIAL, [0, 0], [1, 0], [0, 255]);
			g.drawCircle(0, 0, 100);
			FP.matrix.identity();
			FP.matrix.scale(FP.width / 200, FP.height / 200);
			FP.matrix.translate(FP.width / 2, FP.height / 2);
			data.draw(FP.sprite, FP.matrix);
			g.clear();
			g.beginBitmapFill((new Assets.SPLASH_LINES).bitmapData);
			g.drawRect(0, 0, FP.width, FP.height);
			data.draw(FP.sprite);
			lines = new Image(data);
			
			// Set the entity information.
			x = FP.width / 2;
			y = FP.height / 2;
			graphic = new Graphiclist(explosiveBrains, explosiveGame, lines, fade);
			
			// Set the screen information.
			FP.screen.color = bgColor;
			
			// Set the lines properties.
			lines.blend = BlendMode.SUBTRACT;
			lines.smooth = true;
			lines.x -= x;
			lines.y -= y;
			
			// Set the fade cover properties.
			fade.x -= x;
			fade.y -= y;
            
            //Configuramos la presentación del estudio:
            explosiveBrains.x = -FP.width/2;
            explosiveBrains.y = -FP.height/2;
            explosiveBrains.visible = true;
            
            //Configuramos la presentación del juego:
            explosiveGame.x = -FP.width/2;
            explosiveGame.y = -FP.height/2;
            explosiveGame.visible = false;
			
			// Set the timing properties.
			_fadeTime = Global.tiempoFade;
			_pauseTimer  = Global.tiempoPausa;
			
			// Add the tweens.
			addTween(fader);
			
			// Make invisible until you start it.
			visible = false;
		}
		
		/**
		 * Start the splash screen.
		 */
		public function start(onComplete:* = null):void {
			_onComplete = onComplete;
			visible = true;
			fadeIn();
		}
		
		/**
		 * Update the splash screen.
		 */
		override public function update():void  {
			// Fade in/out alpha control.
			fade.alpha = fader.value;
			
			// Pause before fade out.
			if (_pauseTimer > 0)
                _pauseTimer--;
            else if (!fader.active && !fader.value)
                fadeOut();
		}
		
		/**
		 * When the fade tween completes.
		 */
		private function faderEnd():void {
			if (fader.value && times) {
                explosiveBrains.visible = false;
                explosiveGame.visible = true;
                times--;
                _pauseTimer  = Global.tiempoPausa;
                fadeIn();
            }
			else if (fader.value && !times)
                splashEnd();
		}
				
		/**
		 * When the splash screen has completed.
		 */
		private function splashEnd():void {
			if (_onComplete == null) return;
			else if (_onComplete is Function) _onComplete();
			else if (_onComplete is World) FP.world = _onComplete;
			else throw new Error("The onComplete parameter must be a Function callback or World object.");
		}
		
		/**
		 * Fades the splash screen in.
		 */
		private function fadeIn():void {
			fader.tween(1, 0, _fadeTime, Ease.cubeOut);
		}
		
		/**
		 * Fades the splash screen out.
		 */
		private function fadeOut():void {
			fader.tween(0, 1, _fadeTime, Ease.cubeIn);
		}
	}
}






















//